// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.butler.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.constant.ThreadLocalConstants;
import com.liuxinlong.modules.butler.controller.request.QueryFaqReq;
import com.liuxinlong.modules.butler.controller.request.UpdateCommentReq;
import com.liuxinlong.modules.butler.service.ButlerService;
import com.liuxinlong.modules.butler.service.CommentService;
import com.liuxinlong.modules.butler.service.FaqService;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.Comment;
import com.liuxinlong.modules.entity.User;
import com.liuxinlong.utils.ThreadlocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学习中心控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022-12-05
 */
@RestController
@RequestMapping("/study")
public class StudyController {

    @Autowired
    private FaqService faqService;

    @Autowired
    private ButlerService butlerService;

    @Autowired
    private CommentService commentService;

    /**
     * 分页查询faq信息
     *
     * @param req 请求信息
     * @return faq信息列表
     */
    @PostMapping("faq/page")
    public BaseResp pageFaqInfo(@RequestBody QueryFaqReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("keyword", req.getKeyword());
        queryMap.put("subType", req.getSubType());
        List<Map<String, Object>> dataList = faqService.pageQuestionAnswer(queryMap);
        int totalNum = faqService.getQuestionAnswerCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询FAQ信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 分页查询培训文档信息
     *
     * @param req 请求信息
     * @return 培训文档信息列表
     */
    @PostMapping("train/page")
    public BaseResp pageButlerTrain(@RequestBody QueryFaqReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("keyword", req.getKeyword());
        queryMap.put("status", req.getStatus());
        queryMap.put("businessType", req.getBusinessType());
        queryMap.put("subType", req.getSubType());
        List<Map<String, Object>> dataList = butlerService.pageTrainInfo(queryMap);
        int totalNum = butlerService.getTrainInfoCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询培训文档信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 查询培训信息
     *
     * @param req 请求信息
     * @return 培训信息
     */
    @PostMapping("train/query")
    public BaseResp queryTrain(@RequestBody BaseDeleteReq req) {
        Map<String, Object> queryMap = butlerService.queryTrainInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询培训信息成功");
        resp.setDataMap(queryMap);
        return resp;
    }

    /**
     * 查询faq信息
     *
     * @param req 请求信息
     * @return faq信息
     */
    @PostMapping("faq/query")
    public BaseResp queryFaq(@RequestBody BaseDeleteReq req) {
        Map<String, Object> queryMap = faqService.queryFaqInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询FAQ信息成功");
        resp.setDataMap(queryMap);
        return resp;
    }

    /**
     * 记录点击次数
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("count")
    @LogAnnotation(value = "记录点击次数", business = LogBusinessConstants.BUTLER_MANAGE, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp countClick(@RequestBody BaseDeleteReq req) {
        butlerService.countClick(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("记录点击次数信息成功");
        return resp;
    }

    /**
     * 查询评论信息
     *
     * @param req 请求信息
     * @return 评论信息
     */
    @PostMapping("comment/page")
    public BaseResp pageComment(@RequestBody QueryBaseReq req) {
        Map<String,Object> queryParam = new HashMap<>();
        queryParam.put("startNum",req.getStartNum());
        queryParam.put("pageSize",req.getPageSize());
        queryParam.put("parentId",req.getId());
        User current = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        queryParam.put("currentUser",current.getId());
        List<Map<String, Object>> dataList = commentService.pageCommentInfo(queryParam);
        int totalNum = commentService.getCommentInfoCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询评论信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 新增评论信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("comment/add")
    public BaseResp addComment(@RequestBody UpdateCommentReq req) {
        Comment comment = new Comment();
        comment.setParentId(req.getParentId());
        comment.setReply(req.getReply());
        comment.setContent(req.getContent());
        commentService.addCommentInfo(comment);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增评论信息成功");
        return resp;
    }
}
